<template>
  <div class="wh100 p20">
    <el-form ref="editForm" :model="form" :rules="rules" label-width="140px" label-position="top">
      <el-row class="card-box">
        <el-card v-loading="loading">
          <div slot="header" class="inline-block w100">
            <b>密码策略</b>
            <el-button type="danger" class="fr" plain size="mini" @click="savePassword">保存</el-button>
          </div>
          <el-row>
            <el-col :span="8" :xs="24">
              <el-form-item label="策略开关" prop="enable">
                <el-tooltip :content="'当前状态：' + (form.enable ? '打开' : '关闭')" placement="top">
                  <el-switch v-model="form.enable" active-color="#409EFF" inactive-color="#ff4949" />
                </el-tooltip>
              </el-form-item>
              <el-form-item label="会话超时时间" prop="sessionTime">
                <el-input-number v-model="form.sessionTime" :min="10" :max="180" class="mr5" :disabled="!form.enable" />分钟
              </el-form-item>
              <el-form-item label="密码最长使用天数" prop="passwordMaxUse">
                <el-input-number v-model="form.passwordMaxUse" :min="30" :max="180" class="mr5" :disabled="!form.enable" />天
              </el-form-item>
              <el-form-item label="密码过期提前提醒天数" prop="passwordOutRemindDay">
                <el-input-number v-model="form.passwordOutRemindDay" :min="2" :max="10" class="mr5" :disabled="!form.enable" />天
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item label="用户默认密码" prop="defaultPassword">
                <el-input v-model="form.defaultPassword" clearable type="password" maxlength="15" class="w250" placeholder="请输入" :disabled="!form.enable" />
              </el-form-item>
              <el-form-item label="密码最小长度" prop="minLength">
                <el-input-number v-model="form.minLength" :min="6" :max="15" class="mr5" :disabled="!form.enable" />
              </el-form-item>
              <el-form-item label="密码强度" prop="passwordStronger">
                <el-radio v-model="form.passwordStronger" :label="1" :disabled="!form.enable">弱</el-radio>
                <el-radio v-model="form.passwordStronger" :label="2" :disabled="!form.enable">中</el-radio>
                <el-radio v-model="form.passwordStronger" :label="3" :disabled="!form.enable">强</el-radio>
              </el-form-item>
              <el-form-item label="修改密码不重复次数" prop="repeatCount">
                <el-input-number v-model="form.repeatCount" :min="0" :max="5" class="mr5" :disabled="!form.enable" />天
              </el-form-item>

            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item label="验证码开关" prop="captchaEnable" maxlength="5">
                <el-tooltip :content="'当前状态：' + (form.captchaEnable ? '打开' : '关闭')" placement="top">
                  <el-switch v-model="form.captchaEnable" active-color="#409EFF" inactive-color="#ff4949" :disabled="!form.enable" />
                </el-tooltip>
              </el-form-item>
              <el-form-item label="验证码类型" prop="captchaType">
                <el-radio v-model="form.captchaType" label="math" :disabled="!form.enable">数学计算</el-radio>
                <el-radio v-model="form.captchaType" label="char" :disabled="!form.enable">静态字符</el-radio>
              </el-form-item>
              <el-form-item label="密码最大错误次数" prop="passwordMaxErrorCount">
                <el-input-number v-model="form.passwordMaxErrorCount" :min="2" :max="5" class="mr5" :disabled="!form.enable" />
              </el-form-item>
              <el-form-item label="密码错误锁定时长" prop="passwordMaxErrorTime">
                <el-input-number v-model="form.passwordMaxErrorTime" :min="3" :max="72*60" class="mr5" :disabled="!form.enable" />分钟
              </el-form-item>

            </el-col>
          </el-row>
        </el-card>
      </el-row>

      <el-row class="card-box">
        <el-card v-loading="loading">
          <div slot="header" class="inline-block w100">
            <b>系统安全</b>
            <el-button type="danger" plain size="mini" class="fr" @click="saveSystem">保存</el-button>
          </div>
          <el-row>
            <el-col :span="6" :xs="24">
              <el-form-item label="允许跨域请求" prop="crossOrigin">
                <el-tooltip :content="'当前状态：' + (form.crossOrigin ? '允许' : '不允许')" placement="top">
                  <el-switch v-model="form.crossOrigin" active-color="#409EFF" inactive-color="#ff4949" />
                </el-tooltip>
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="允许从其他网站跳转到本系统" prop="refererCheck">
                <el-tooltip :content="'当前状态：' + (form.refererCheck ? '检查' : '不检查')" placement="top">
                  <el-switch v-model="form.refererCheck" active-color="#409EFF" inactive-color="#ff4949" />
                </el-tooltip>
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="允许网站被其他系统嵌套在iframe" prop="xFrameOptions">
                <el-tooltip :content="'当前状态：' + (form.xFrameOptions ? '允许' : '不允许')" placement="top">
                  <el-switch v-model="form.xFrameOptions" active-color="#409EFF" inactive-color="#ff4949" />
                </el-tooltip>
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="记录请求地理位置" prop="location">
                <el-tooltip :content="'当前状态：' + (form.location ? '记录' : '不记录')" placement="top">
                  <el-switch v-model="form.location" active-color="#409EFF" inactive-color="#ff4949" />
                </el-tooltip>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
      </el-row>
    </el-form>
  </div>

</template>

<script>
import { validator } from '@/utils/form'
import { getSettingMap, savePasswordPolicy, saveSystemPolicy } from '@/api/sys'
export default {
  name: 'Setting',
  components: { },
  data() {
    return {
      par: {
        module: '',
        name: ''
      },
      modules: [],
      form: {
        enable: false,
        captchaEnable: false,
        sessionTime: 30,
        minLength: 6,
        repeatCount: 3,
        passwordMaxErrorCount: 3,
        passwordMaxErrorTime: 5,
        passwordOutRemindDay: 3,
        passwordStronger: 1,
        passwordMaxUse: 180,
        defaultPassword: null,
        captchaType: 'math',
        crossOrigin: true,
        xFrameOptions: true,
        refererCheck: true,
        location: true
      },
      title: '修改参数',
      add: false,
      loading: false,
      show: false,
      showEncrypt: false,
      labelWidth: '80px'
    }
  },
  computed: {
    rules() {
      return {
        defaultPassword: [
          { validator: validator('notBlank'), message: '默认密码不能为空', trigger: ['blur', 'change'] }
        ],
        sessionTime: [
          { validator: validator('notBlank'), message: '会话超时时间不能为空', trigger: ['blur', 'change'] }
        ],
        minLength: [
          { validator: validator('notBlank'), message: '密码最小长度不能为空', trigger: ['blur', 'change'] }
        ],
        repeatCount: [
          { validator: validator('notBlank'), message: '修改密码不重复次数不能为空', trigger: ['blur', 'change'] }
        ],
        passwordMaxErrorCount: [
          { validator: validator('notBlank'), message: '密码最大错误次数不能为空', trigger: ['blur', 'change'] }
        ],
        passwordMaxErrorTime: [
          { validator: validator('notBlank'), message: '密码错误锁定时长不能为空', trigger: ['blur', 'change'] }
        ],
        passwordOutRemindDay: [
          { validator: validator('notBlank'), message: '密码过期提前提醒天数不能为空', trigger: ['blur', 'change'] }
        ],
        passwordMaxUse: [
          { validator: validator('notBlank'), message: '密码最长使用天数不能为空', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  mounted() {
    this.initPolicySetting()
    this.initSystemSetting()
  },
  methods: {
    initPolicySetting() {
      this.loading = true
      getSettingMap('password-policy').then((r) => {
        this.loading = false
        const d = r
        this.form.enable = d.enable === 'true'
        this.form.captchaEnable = d.captchaEnable === 'true'
        this.form.sessionTime = parseInt(d.sessionTime)
        this.form.minLength = parseInt(d.minLength)
        this.form.repeatCount = parseInt(d.repeatCount)
        this.form.passwordMaxErrorCount = parseInt(d.passwordMaxErrorCount)
        this.form.passwordMaxErrorTime = parseInt(d.passwordMaxErrorTime)
        this.form.passwordOutRemindDay = parseInt(d.passwordOutRemindDay)
        this.form.passwordStronger = parseInt(d.passwordStronger)
        this.form.passwordMaxUse = parseInt(d.passwordMaxUse)
        this.form.defaultPassword = d.defaultPassword
        this.form.captchaType = d.captchaType
        console.log(r)
      })
    },
    initSystemSetting() {
      this.loading = true
      getSettingMap('system').then((r) => {
        this.loading = false
        const d = r
        this.form.refererCheck = d.refererCheck === 'true'
        this.form.xFrameOptions = d.xFrameOptions === 'true'
        this.form.location = d.location === 'true'
        this.form.crossOrigin = d.crossOrigin === 'true'
        console.log(r)
      })
    },
    saveSystem() {
      this.loading = true
      saveSystemPolicy(this.form).then((r) => {
        this.$message.success('保存成功')
        this.loading = false
        this.initSystemSetting()
      }).catch(() => {
        this.loading = false
      })
    },
    savePassword() {
      this.$refs.editForm.validate(r => {
        if (r) {
          this.loading = true
          savePasswordPolicy(this.form).then((r) => {
            this.$message.success('保存成功')
            this.loading = false
            this.initPolicySetting()
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },

  }
}
</script>
<style scoped>
.card-box {
  padding-right: 15px;
  padding-left: 15px;
  margin-bottom: 10px;
}
</style>
